﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.web;
using sfApi = SocialFront.Umbraco;

namespace SocialFront.Web.umbraco.plugins.SocialFront
{
    public partial class SetupSocialFront : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Add the root node first
                TreeNode rootNode = new TreeNode()
                                  {
                                      Text = "Content",
                                      Value = "-1"
                         };

                //TreeView1.Nodes.Add(rootNode);

                // Now get the children of the root node and recursively add them to the tree
                Document[] documents = Document.GetRootDocuments();

                TreeNode tn = new TreeNode();

                foreach (var document in documents)
                {
                    tn.Text = document.Text;
                    tn.Value = document.Id.ToString();
                    tn = getChildDocuments(document, tn);
                }
                //documentOutput.Text = document.ge;
                rootNode.ChildNodes.Add(tn);
                TreeView1.Nodes.Add(rootNode);
                //TreeView1.DataBind();
            }
        }

        private TreeNode getChildDocuments(Document doc, TreeNode treeNode)
        {
            //treeNode.ChildNodes.Add(new TreeNode(doc.Text, doc.Id.ToString()));
            if (doc.HasChildren)
            {
                foreach (Document childDoc in doc.Children)
                {
                    TreeNode newNode = new TreeNode(childDoc.Text, childDoc.Id.ToString());
                    treeNode.ChildNodes.Add(getChildDocuments(childDoc, newNode));
                }
            }
            return treeNode;
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Wizard1.ActiveStepIndex = 3;
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if(e.CurrentStepIndex == 0)
            {
                if(!isTreeNodeSelected())
                {
                    Wizard1.ActiveStepIndex = 0;
                    lStep1Error.Visible = true;
                    lStep1Error.Text = "Please select a node from the list below.";
                    return;
                }
                else
                {
                    sfApi.Install.CreateCommunityArea(int.Parse(TreeView1.SelectedValue), Server.MapPath("/"));

                    Wizard1.ActiveStepIndex = 1;
                    lblInfo.Text = "Stuff done, in theory.";
                }
            }
        }

        private bool isTreeNodeSelected()
        {
            if(!string.IsNullOrEmpty(TreeView1.SelectedValue))
            {
                return true;
            }
            return false;
        }
        
    }
}